﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Limilabs.Client.IMAP;
using Limilabs.Mail;
using Limilabs.Mail.MIME;
using Limilabs.Mail.Headers;
using Limilabs.Client.POP3;
using System.Threading.Tasks;
   

//-Account for empty inbox

namespace PennApps
{
    public partial class TransportService
    {
        private static string infoUsername = "momapp2013";
        private static string infoPassword = "liangluo";
        private static string commandUsername = "momapp2013commands";
        private static string commandPassword = "liangluo";

        public static ProcessPerformanceInformation[] ReceiveInfo(Boolean deleteEmail)
        {
            //create array for bodies of emails and get the bodies of emails as string
            string body;
            body = getContent(infoUsername, infoPassword, deleteEmail);
            //create the array of information objects based on the string array
            ProcessPerformanceInformation[] infoObjects;
            PerformanceInfoToTextConversionEngine converter;
            converter = new PerformanceInfoToTextConversionEngine();
            Console.WriteLine("this is the body to be converted: " + body);
            infoObjects = converter.ConvertBack(body);
            return infoObjects;
        }
        //get text of first email and DELETE
        public static Command ReceiveCommand(Boolean deleteEmail)
        {
            PerformanceInfoToTextConversionEngine converter = new PerformanceInfoToTextConversionEngine();
            string body = getContent(commandUsername, commandPassword, deleteEmail);
            Console.WriteLine(body);
            Command commandObj = converter.CommandConvert(body);
            Console.WriteLine(commandObj.ProcessName);
            return commandObj;
        }
        
        public static string getContent(string username, string password, Boolean deleteReadEmail)
        {
            using (Imap imap = new Imap())
            {
                imap.ConnectSSL("imap.gmail.com", 993);    // use ConnectSSL for SSL
                imap.UseBestLogin(username, password);
                imap.SelectInbox();
                List<long> uids = imap.Search(Flag.All);
                string body;
                if (uids.Count != 0)
                {
                    string eml = imap.GetMessageByUID(uids[uids.Count - 1]);
                    IMail email = new MailBuilder()
                        .CreateFromEml(eml);
                    // Console.WriteLine(email.Subject);
                    body = email.Text;
                    // Console.WriteLine(body);
                    if (deleteReadEmail)
                    {
                        imap.DeleteMessageByUID(uids[uids.Count - 1]);
                    }
                    imap.Close();
                }
                else
                {
                    body = null;
                }

                return body;
            }

        }

       
    }
}
